﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.View.Base;


namespace HR60_PanelSoft.View.TabForms.ListItems
{
    //GC: [g1] 修正场景编辑不保存到内存 [SceneAttrItem]@s1

	public partial class SceneAttrItem : BaseFormControl
	{
		CtrlAttr ctrlAttr = null;

		public SceneAttrItem(CtrlAttr ctrlAttr)
		{
			InitializeComponent();

			this.ctrlAttr = ctrlAttr;
			cbEnabled.Checked = false;
			txtName.Text = ctrlAttr.name;
			selValue.bindData(ctrlAttr.values);


            //cbEnabled.CheckedChanged += new EventHandler(valueChange);
            //selValue.ValueChanged += new EventHandler(valueChange);
            selValue.ValueChanged += new ValueChangedHandler(valueChange);
            cbEnabled.CheckedChanged += new EventHandler(valueChange_Normal);
		}

		// 目标设备
		public CtrlAttr TagAttr
		{
			get
			{
				return ctrlAttr;
			}
		}

		// 重写Value属性，返回属性Value的Key
		public override object Value
		{
			get
			{
				if (cbEnabled.Checked)
				{
                    /* [g2.s2]修正可控属性选择值的返回 */
					//return selValue.Text;
                    if (null == selValue.SelectedItem)
                        return null;
                    return   selValue.SelectedItem.value;
                    /* end of [g2.s2] */
				}
				else
				{
					return null;
				}
			}
			set
			{
				if (value == null)
				{
					cbEnabled.Checked = false;
				}
				else
				{
					selValue.Text = (string) value;
				}
			}
		}

        /*[g1.s1]: 增加更新值函数 */
        /// <summary>
        /// 更新值，由于Value属性的存储不能满足更新需求
        /// 相比较而言，Value只适合进行初始化时设置
        /// 注意：使用前，确保正确关联SceneEvent的device
        /// </summary>
        /// <param name="se">源数据</param>
        public void LoadValue(SceneEvent se)
        {
            if (null == se)
                return;
            /*1. 先清除关联，并初始化 */
            cbEnabled.Checked = false;
            txtName.Text = ctrlAttr.name;
            selValue.bindData(ctrlAttr.values);
            //int idx = se.attrs.FindIndex(x => x == selValue.Text);
            int idx = se.attrs.IndexOf(txtName.Text);
            if (idx < 0 || idx >= se.attrs.Count)
                return;
            selValue.Value = se.attrValueKeys[idx];
            cbEnabled.Checked = true;
        }
        /* end of [g1.s1] */



	}
}
